/**
 *     tipple is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    tipple is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with tipple.  If not, see <http://www.gnu.org/licenses/>.
 * Copyright 2009 James Osler, Adam Tomkins

 */



package v0_3;

//~--- JDK imports ------------------------------------------------------------

import java.util.*;

public class FindBest {
    List<Beer>      beerList;    // where we are going to store the list
    private boolean j;
    private double  minCost;
    private double  remCash;     // remaining cash user has
    List<Beer>      suggestedList;

    /**
     * Constructor that needs an arraylist of beers and the total cash
     */
    FindBest(List<Beer> incBeerList) {
        beerList = incBeerList;
    }

    /**
     * Method to sort the list, arranging in order of HU
     */
    public List<Beer> sortADC() {
        Beer tempBeer;    // temporary value that we use to store a beer that we're moving position in.

        tempBeer = new Beer();

        for (int u = 1; u < beerList.size() + 1; u++) {        // for each beer in the arraylist, put it in order
            Beer max    = beerList.get(u - 1);                 // the beer with maximum ADC
            int  maxPos = u - 1;                               // the position in the array of the maximum ADC beer

            for (int i = u; i < beerList.size() + 1; i++) {    // for each beer, check it against the ones after it (unsorted beers)
                if (beerList.get(i - 1).getADC() > max.getADC()) {    // record which is the maximum of the remainder
                    max    = beerList.get(i - 1);
                    maxPos = i - 1;
                }
            }

            tempBeer = beerList.get(u - 1);    // put the beer to be moved out of way of the next max beer in temp value
            beerList.set(u - 1, max);                                 // put the next max beer in it's place
            beerList.set(maxPos, tempBeer);    // put the temporary beer where the max beer was moved from
        }

        return beerList;
    }

    /**
     * Sorts in order of cost.
     */
    public List<Beer> sortCost() {
        Beer tempBeer;    // temporary value that we use to store a beer that we're moving position in.

        tempBeer = new Beer();

        for (int u = 1; u < beerList.size() + 1; u++) {        // for each beer in the arraylist, put it in order
            Beer max    = beerList.get(u - 1);                 // the beer with maximum ADC
            int  maxPos = u - 1;                               // the position in the array of the maximum ADC beer

            for (int i = u; i < beerList.size() + 1; i++) {    // for each beer, check it against the ones after it (unsorted beers)
                if (beerList.get(i - 1).getCost() > max.getCost()) {    // record which is the maximum of the remainder
                    max    = beerList.get(i - 1);
                    maxPos = i - 1;
                }
            }

            tempBeer = beerList.get(u - 1);    // put the beer to be moved out of way of the next max beer in temp value
            beerList.set(u - 1, max);                                   // put the next max beer in it's place
            beerList.set(maxPos, tempBeer);    // put the temporary beer where the max beer was moved from
        }

        minCost = beerList.get(beerList.size() - 1).getCost();

        return beerList;
    }

    /**
     * Suggest what beers to buy!
     */
    public List<Beer> suggest(double incTotalCash, boolean checked) {
        double totalCash = incTotalCash;

        remCash       = totalCash;
        suggestedList = new ArrayList<Beer>();
        sortADC();

        for (int i = 1; i < beerList.size() + 1; i++) {
            if (remCash - beerList.get(i - 1).getCost() < 0) {
                break;
            }

            remCash = remCash - beerList.get(i - 1).getCost();

            double retCash = (int) (remCash * 10000);

            remCash = retCash / 10000;
            suggestedList.add(beerList.get(i - 1));

            if (remCash <= totalCash / 4) {
                break;
            }
        }

        sortCost();

        while (remCash >= minCost) {
            for (int i = 1; i < beerList.size() + 1; i++) {
                double retCash = (int) (remCash * 10000);

                remCash = retCash / 10000;

                if (remCash == beerList.get(i - 1).getCost()) {
                    suggestedList.add(beerList.get(i - 1));
                    remCash = 0;
                } else if (remCash - beerList.get(i - 1).getCost() > 0) {
                    j = true;

                    for (int l = 1; l < suggestedList.size() + 1; l++) {
                        if (suggestedList.get(l - 1).getid() == beerList.get(i - 1).getid()) {
                            j = false;

                            if (checked == true) {
                                suggestedList.get(l - 1).addBuyCount();
                                remCash = remCash - suggestedList.get(l - 1).getCost();
                                retCash = (int) (remCash * 10000);
                                remCash = retCash / 10000;
                            }
                        }
                    }

                    if (j != false) {
                        remCash = remCash - beerList.get(i - 1).getCost();
                        retCash = (int) (remCash * 10000);
                        remCash = retCash / 10000;
                        suggestedList.add(beerList.get(i - 1));
                    }
                }
            }

            if (checked == false) {
                break;
            }
        }

        return suggestedList;
    }

    public List<Beer> suggest2(double incTotalCash, boolean checked) {
        double totalCash = incTotalCash;

        remCash       = totalCash;
        suggestedList = new ArrayList<Beer>();
        sortADC();

        for (int i = 0; i < beerList.size(); i++) {
            if ((remCash - beerList.get(i).getCost()) < 0) {
                break;
            }

            suggestedList.add(beerList.get(i));
            remCash = remCash - beerList.get(i).getCost();

            double retCash = (int) (remCash * 10000);

            remCash = retCash / 10000;
        }

        if (checked == true) {
            for (int i = 0; i < beerList.size(); i++) {
                if ((remCash - beerList.get(i).getCost()) < 0) {
                    break;
                }

                suggestedList.get(i).addBuyCount();
                remCash = remCash - beerList.get(i).getCost();

                double retCash = (int) (remCash * 10000);

                remCash = retCash / 10000;
            }
        }

        return suggestedList;
    }

    /**
     * Getter for the remaining cash.
     */
    public double getRemCash() {
        return remCash;
    }

    /**
     * Getter for total HU in suggested list.
     */
}


//~ Formatted by Jindent --- http://www.jindent.com
